﻿Partial Class Document

    Public Class Table
        Public Section As Document.Section
        Public Columns As New List(Of Column)
        Public HeaderRow As Row
        Public FontSize As String = ""

        Dim Rows As New List(Of Row)
        Dim TotalSize As Integer = 0

        Sub New(ByVal Section As Document.Section, ByVal FontSize As String)
            Me.Section = Section
            Me.FontSize = FontSize
        End Sub

        Sub AddColumns(ByVal ColumnDefinitions As String)
            Dim Text As String = ""
            For Each Column As String In Split(ColumnDefinitions, ",")
                Dim Item() As String = Split(";;;" & Column, ";")
                Dim i As Integer = Item.Length
                Dim C As New Column(Item(i - 3), Item(i - 2), Item(i - 1))
                Columns.Add(C)
                Text &= ";" & C.Caption
                TotalSize += Item(i - 2)
            Next
            If Replace(Text, ";", "") = "" Then Exit Sub
            HeaderRow = New Row(Me, True)
            HeaderRow.SetValues(Split(Mid(Text, 2), ";"))
        End Sub

        Sub AddRow(ByVal ParamArray Params() As Object)
            Dim Row As New Row(Me)
            Row.SetValues(Params)
            Rows.Add(Row)
        End Sub

        Sub SkipLine(Optional ByVal Count As Integer = 1)
            For i As Integer = 1 To Count
                Dim Row As New Row(Me)
                Row.SetValues(" ")
                Rows.Add(Row)
            Next
        End Sub

        Sub Print(ByVal Info As PrintInfo)
            Dim Left As Integer = Info.PrintableArea.Left
            Dim Right As Integer = Info.PrintableArea.Right
            Dim Width As Integer = Right - Left
            For Each Column As Column In Columns
                Column.Left = Left
                Column.Width = Column.Size / TotalSize * Width
                Left += Column.Width
            Next
            If Not HeaderRow Is Nothing Then HeaderRow.Print(Info)
            For Each Row As Row In Rows
                Row.Print(Info)
            Next
        End Sub

    End Class

End Class
